import sys
import os
import logging

import wx

from view_data.utils import setup_logger
from view_data.controller import Controller

if wx.Platform == '__WXGTK__':
    default_database_path = '/experiment_data/stored_data/database.sqlite'
else:
    default_database_path = None

if __name__ == '__main__':
    setup_logger()
    logger = logging.getLogger('View_data')
    app = wx.PySimpleApp()

    controller = Controller()
    controller.setup()
    controller.setup_subscriptions()
    
    # check to see if a path was supplied at the command line, if so
    #   automatically add the database found there to the tree_panel.
    dbpath = default_database_path
    passed_dbpath = None
    for arg in sys.argv:
        if '-dbp' == arg:
            passed_dbpath = sys.argv[sys.argv.index(arg)+1]
        if '--dbpath' in arg:
            junk, passed_dbpath = arg.split('=')
    if passed_dbpath is not None and os.path.exists(passed_dbpath):
        dbpath = passed_dbpath
    else:
        logger.debug('Argument passed for database (%s) does not exist.' % passed_dbpath)

    controller.model.open_database(dbpath)

    app.MainLoop()
